package com.example.springboot_vue.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_vue.common.PageDto;
import com.example.springboot_vue.entity.UserEntity;
import com.example.springboot_vue.mapper.UserMapper;
import com.example.springboot_vue.req.UserLoginReq;
import com.example.springboot_vue.resp.UserLoginResp;
import com.example.springboot_vue.service.UserService;
import com.example.springboot_vue.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Author：sgp
 * @Package：com.example.springboot_vue.mapper.Impl
 * @Project：springboot_vue
 * @name：UserServiceImpl
 * @Date：2024/7/12 17:53
 * @Filename：UserServiceImpl
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageDto<UserEntity> getList(UserEntity userEntity) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(userEntity.getName())){
            queryWrapper.lambda().like(UserEntity::getName,userEntity.getName());
        }
        if(ObjectUtils.isNotEmpty(userEntity.getSex())){
            queryWrapper.lambda().eq(UserEntity::getSex,userEntity.getSex());
        }
        if(ObjectUtils.isNotEmpty(userEntity.getRoleId())){
            queryWrapper.lambda().eq(UserEntity::getRoleId,userEntity.getRoleId());
        }
        Page<UserEntity> page = new Page<>(userEntity.getPage(), userEntity.getSize());
        Page<UserEntity> selectPage = userMapper.selectPage(page, queryWrapper);
        PageDto<UserEntity> pageDto = new PageDto<>();
        pageDto.setTotal(selectPage.getTotal());
        pageDto.setList(selectPage.getRecords());
        return pageDto;
    }

    @Override
    public UserEntity findByNo(String no) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserEntity::getNo,no);
        UserEntity user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public UserLoginResp login(UserLoginReq req) {
        UserEntity userDb = selectByLoginName(req.getNo());
        if(ObjectUtils.isEmpty(userDb)){
            return null;//用防护不存在
        }else {
            UserLoginResp userLoginResp = CopyUtil.copy(userDb, UserLoginResp.class);
            return userLoginResp;
        }
    }

//    先来查询loginName是否被注册过
    public UserEntity selectByLoginName(String no){
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserEntity::getNo,no);
        List<UserEntity> userEntities = userMapper.selectList(wrapper);
        if(ObjectUtils.isEmpty(userEntities)){
                return null;
        }else {
            return userEntities.get(0);
        }

    }
}
